package cn.t.repository.impl.order;

import cn.t.constants.ParamConstants;
import cn.t.converter.order.OverPickupRecordConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.dto.order.OrderQueryRequest;
import cn.t.model.order.OverPickupRecordDO;
import cn.t.model.order.OverPickupRecordVO;
import cn.t.persistence.entity.order.OverPickupRecordPO;
import cn.t.persistence.mapper.order.OverPickupRecordMapper;
import cn.t.repository.order.OverPickupRecordRepository;
import cn.t.utils.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 超领记录表(OverPickupRecord)数据仓库
 *
 * @author t
 * @since 2024-05-24 10:18:10
 */
@Slf4j
@Repository
@AllArgsConstructor
public class OverPickupRecordRepositoryImpl extends RepositoryImpl<OverPickupRecordMapper, OverPickupRecordPO, OverPickupRecordConverter, OverPickupRecordDO> implements OverPickupRecordRepository {


    @Override
    public List<OverPickupRecordDO> findByModel(OverPickupRecordDO overPickupRecordDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = getLastSql(start, pageSize, field, direction);

        List<OverPickupRecordPO> list = getWrapper(overPickupRecordDO)
                .last(lastSql)
                .list();
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    @Override
    public long count(OverPickupRecordDO dataDo) {
      return getWrapper(dataDo).count();
    }


    private static String getLastSql(int start, Integer pageSize, String field, String direction) {
        return StringUtils.hasText(field) ?
                " order by " + field +" "+ direction + " limit " + start + "," + pageSize
                :
                " limit " + start + "," + pageSize;
    }

    private LambdaQueryChainWrapper<OverPickupRecordPO> getWrapper(OverPickupRecordDO overPickupRecordDO) {
        LambdaQueryChainWrapper<OverPickupRecordPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(overPickupRecordDO.getOrderSn())) {
            wrapper.eq(OverPickupRecordPO::getOrderSn, overPickupRecordDO.getOrderSn());
        }
        if (!ObjectUtils.isEmpty(overPickupRecordDO.getTimeLimitType())) {
            wrapper.eq(OverPickupRecordPO::getTimeLimitType, overPickupRecordDO.getTimeLimitType());
        }
        if (!ObjectUtils.isEmpty(overPickupRecordDO.getFirmId())) {
            wrapper.eq(OverPickupRecordPO::getFirmId, overPickupRecordDO.getFirmId());
        }
        if (!ObjectUtils.isEmpty(overPickupRecordDO.getQuantityLimit())) {
            wrapper.eq(OverPickupRecordPO::getQuantityLimit, overPickupRecordDO.getQuantityLimit());
        }
        if (!ObjectUtils.isEmpty(overPickupRecordDO.getPickupQty())) {
            wrapper.eq(OverPickupRecordPO::getPickupQty, overPickupRecordDO.getPickupQty());
        }
        if (!ObjectUtils.isEmpty(overPickupRecordDO.getOverQty())) {
            wrapper.eq(OverPickupRecordPO::getOverQty, overPickupRecordDO.getOverQty());
        }
        if (!ObjectUtils.isEmpty(overPickupRecordDO.getSkuCode())) {
            wrapper.eq(OverPickupRecordPO::getSkuCode, overPickupRecordDO.getSkuCode());
        }
        if (!ObjectUtils.isEmpty(overPickupRecordDO.getSkuName())) {
            wrapper.eq(OverPickupRecordPO::getSkuName, overPickupRecordDO.getSkuName());
        }
        if (!ObjectUtils.isEmpty(overPickupRecordDO.getGmtModified())) {
            wrapper.between(OverPickupRecordPO::getGmtCreated, overPickupRecordDO.getGmtModifiedStart(), overPickupRecordDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(overPickupRecordDO.getGmtCreated())) {
            wrapper.between(OverPickupRecordPO::getGmtCreated, overPickupRecordDO.getGmtCreatedStart(), overPickupRecordDO.getGmtCreatedEnd());
        }
        return wrapper;
    }

    @Override
    public long countQuery(OrderQueryRequest overPickupRecordDO) {
        QueryWrapper<OverPickupRecordPO> queryWrapper = getQueryWrapper(overPickupRecordDO);
        return baseMapper.queryOverPickupRecordList(queryWrapper).size();
    }
    public QueryWrapper<OverPickupRecordPO>  getQueryWrapper(OrderQueryRequest orderQueryRequest) {

        QueryWrapper<OverPickupRecordPO> queryWrapper = new QueryWrapper<>();

        if (orderQueryRequest.getFirmId() != null) {
            queryWrapper.eq("o.firm_id", orderQueryRequest.getFirmId());
        }
        if (ObjectUtils.isNotEmpty(orderQueryRequest.getSkuCode())) {
            queryWrapper.eq("pr.sku_code", orderQueryRequest.getSkuCode());
        }
        if (ObjectUtils.isNotEmpty(orderQueryRequest.getSkuName())) {
            queryWrapper.eq("pr.sku_name", orderQueryRequest.getSkuName());
        }
        if (orderQueryRequest.getGmtStart() != null && orderQueryRequest.getGmtEnd() != null) {
            queryWrapper.between("o.gmt_created", DateUtil.startDate(orderQueryRequest.getGmtStart()), DateUtil.endDate(orderQueryRequest.getGmtEnd()));
        }
        if (ObjectUtils.isNotEmpty(orderQueryRequest.getUserName())) {
            queryWrapper.like("o.user_name", orderQueryRequest.getUserName().trim());
        }
        if (ObjectUtils.isNotEmpty(orderQueryRequest.getCardCode())) {
            queryWrapper.like("o.card_code", orderQueryRequest.getCardCode());
        }
        if (ObjectUtils.isNotEmpty(orderQueryRequest.getWorkOrderSn())) {
            queryWrapper.eq("pr.work_order_sn", orderQueryRequest.getWorkOrderSn().trim());
        }
        if (ObjectUtils.isNotEmpty(orderQueryRequest.getDeviceName())) {
            queryWrapper.like("d.device_name", orderQueryRequest.getDeviceName().trim());
        }
        if (ObjectUtils.isNotEmpty(orderQueryRequest.getOrderSn())) {
            queryWrapper.like("pr.order_sn", orderQueryRequest.getOrderSn().trim());
        }
        return queryWrapper;
    }
    public List<OverPickupRecordVO> queryOverPickupRecordList(OrderQueryRequest orderQueryDO, int start, Integer pageSize, String field, String direction) {
        QueryWrapper<OverPickupRecordPO> queryWrapper = getQueryWrapper(orderQueryDO);
        queryWrapper.last(getLastSql(start, pageSize, field, direction));
        return baseMapper.queryOverPickupRecordList(queryWrapper);
    }
    @Override
    public List<OverPickupRecordVO> queryOverPickupRecordLock(OrderQueryRequest orderQueryDO, int start, Integer pageSize) {
        QueryWrapper<OverPickupRecordPO> queryWrapper = getQueryWrapper(orderQueryDO);
        queryWrapper.last(getLastSql(start, pageSize, ParamConstants.FIELD, "desc"));
        return baseMapper.queryOverPickupRecordLock(queryWrapper);
    }
    @Override
    public long countLockUserQuery(OrderQueryRequest overPickupRecordDO) {
        QueryWrapper<OverPickupRecordPO> queryWrapper = getQueryWrapper(overPickupRecordDO);
        return baseMapper.queryOverPickupRecordList(queryWrapper).size();
    }
   @Override
    public List<OverPickupRecordDO> queryOverPickupByWorkOrderSn(List<String> workOrderSnList) {
        LambdaQueryChainWrapper<OverPickupRecordPO> wrapper = lambdaQuery();
       List<OverPickupRecordPO> list = wrapper.in(OverPickupRecordPO::getWorkOrderSn, workOrderSnList).list();
       return list.stream().map(converter::po2Model).collect(Collectors.toList());
   }
    @Override
    public Boolean addBatch(List<OverPickupRecordDO> exceptionLogList) {
        return saveBatch(exceptionLogList.stream().map(converter::model2Po).collect(Collectors.toList()));
    }
}
